package com.ruoyi.mdkt.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.mdkt.entity.*;
import com.ruoyi.mdkt.entity.vo.EventVo;
import com.ruoyi.mdkt.entity.vo.TaskVo;
import com.ruoyi.mdkt.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.io.Serializable;
import java.util.*;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author gwl
 * @since 2022-10-22
 */
@CrossOrigin

@RestController
@RequestMapping("/mdkt/task")
public class TaskController extends BaseController {

    @Autowired
    private TaskService taskService;
    @Autowired
    private TaskInfoService taskInfoService;
    @Autowired
    private ScoreLogService scoreLogService;
    @Autowired
    private CourseService courseService;
    @Autowired
    private TaskReceiveService taskReceiveService;

    @Autowired
    private CourseStudentService courseStudentService;
    @Autowired
    private RedisCache redisCache;

    /**
     * 删除无用的system.out.println
     * @param taskId
     * @return
     */
    @GetMapping("/getTaskInfo/{taskId}")
    public R getTaskInfo(@PathVariable("taskId") Long taskId) {
        Task task = taskService.getById(taskId);
        System.out.println(taskId);
        return R.ok(task);
    }

    /**
     * 请和上面方法传参保持一致
     * Url中添加参数声明
     * 修改@RequestParam注解为(@PathVariable
     *
     * @param taskId
     * @return
     */
    @GetMapping("/getQuestion")
    public R getQuestion(@RequestParam("taskId") Serializable taskId) {
        LambdaQueryWrapper<TaskInfo> lambdaQueryWrapper = new LambdaQueryWrapper<TaskInfo>();
        lambdaQueryWrapper.eq(TaskInfo::getTaskId, taskId);
        TaskInfo taskInfo = taskInfoService.getOne(lambdaQueryWrapper);
        return R.ok(taskInfo);
    }

    /**
     * 修改@RequestParam注解为(@PathVariable
     *
     * @param courseId
     * @return
     */
    @GetMapping("/getCourseScoreInfo")
    public R getCourseScoreInfo(@RequestParam("courseId") Serializable courseId) {
        LambdaQueryWrapper<ScoreLog> lambdaQueryWrapper = new LambdaQueryWrapper<ScoreLog>();
        lambdaQueryWrapper.eq(ScoreLog::getCourseId, courseId);
        ScoreLog scoreLog = scoreLogService.getOne(lambdaQueryWrapper);
        return R.ok(scoreLog);
    }


    @PostMapping("/save")
    public R save(@RequestBody Task task) {
        taskService.save(task);
        return R.ok();
    }

    @PostMapping("/saveReceive")
    public R saveReceive(@RequestBody List<TaskReceive> list) {
        Integer integer = taskReceiveService.saveTaskReceiveList(list, getUserId());
        if (integer > 0) {
            return R.ok();
        } else {
            return R.ok(integer);
        }
    }

    @PostMapping("/saveTaskInfo")
    public R saveTaskInfo(@RequestBody TaskInfo taskInfo) {
        taskInfoService.save(taskInfo);
        return R.ok();
    }

    @GetMapping("/pageTaskName/{courseId}")
    public R<Page<Task>> pageTaskName(@PathVariable("courseId") Long courseId, Integer pageNum, Integer pageSize) {
        LambdaQueryWrapper<Task> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Task::getCourseId, courseId);
        Page<Task> page = new Page<>(pageNum, pageSize);
        taskService.page(page, lambdaQueryWrapper);
        return R.ok(page);
    }

    @GetMapping("/pageStudentTestInfo/{taskId}")
    public R<Page<TaskReceive>> pageStudentTestInfo(@PathVariable("taskId") Long taskId, Integer pageNum, Integer pageSize) {
        LambdaQueryWrapper<TaskReceive> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(TaskReceive::getTaskId, taskId);
        Page<TaskReceive> page = new Page<>(pageNum, pageSize);
        taskReceiveService.page(page, lambdaQueryWrapper);
        return R.ok(page);
    }

    @GetMapping("/pageTask/{teacherId}")
    public R<Page<Course>> pageTask(@PathVariable("teacherId") Long teacherId, Integer pageNum, Integer pageSize) {
        LambdaQueryWrapper<Course> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Course::getTeacherId, teacherId);
        Page<Course> page = new Page<>(pageNum, pageSize);
        courseService.page(page, lambdaQueryWrapper);
        return R.ok(page);
    }

    @GetMapping("/pageTaskAll/{studentId}")
    public R<List<TaskVo>> pageTaskAll(@PathVariable("studentId") Long studentId, Integer pageNum, Integer pageSize) throws IOException {
        String studentId1=String.valueOf(studentId);
        List<TaskVo> taskVos = taskService.pageTaskAll(studentId1);
        return R.ok(taskVos);
//        LambdaQueryWrapper<CourseStudent> courseStudentLambdaQueryWrapper = new LambdaQueryWrapper<>();
//        courseStudentLambdaQueryWrapper.eq(CourseStudent::getStudentUserId,studentId);
//        List<CourseStudent> list = courseStudentService.list(courseStudentLambdaQueryWrapper);
//        List<Long> ids = new ArrayList<>();
//        if (list.size() == 0){
//            return R.ok();
//        }
//        list.stream().forEach((item)->{
//            ids.add(item.getCourseId());
//        });
//        LambdaQueryWrapper<Task> wrapper = new LambdaQueryWrapper<>();
//        wrapper.in(Task::getCourseId,ids);
//        Page<Task> page = new Page<>(pageNum,pageSize);
//        taskService.page(page,wrapper);
//        return R.ok(page);
    }

    @PostMapping("/saveTitle")
    public R saveTitle(@RequestBody List<TaskInfo> list) {
        return taskInfoService.saveTitle(list);
    }

}

